/**
 * 
 */
package fr.toto.menuitem;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JMenuItem;

import com.kokotchy.betaSeriesAPI.model.Episode;

import fr.toto.exceptions.ProcessException;
import fr.toto.triseries.Regex;

/**
 * @author tshepo
 * 
 */
public class WebSearchMenuItem extends JMenuItem {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2107243893075273905L;
	private String url;

	/**
	 * 
	 */
	public WebSearchMenuItem(Episode e, String name, String cond) {
		super(name);

		url = "http://www.google.fr/search?q="
				+ e.getShow()
				+ String.format(" S%02dE%02d OR %02dx%02d ", e.getSeason(),
						e.getEpisode(), e.getSeason(), e.getEpisode()) + cond;

		this.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					URI uri = new URI(url.replace(" ", "+"));
					Desktop.getDesktop().browse(uri);
				} catch (Exception e1) {
					new ProcessException(e1);
				}
			}
		});

	}

	public WebSearchMenuItem(Episode ep, String name, String url2, String format) {
		super(name);
		
		url = url2 + Regex.printEpisode(ep, format);

		this.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					URI uri = new URI(url.replace(" ", "+"));
					Desktop.getDesktop().browse(uri);
				} catch (Exception e1) {
					new ProcessException(e1);
				}

			}
		});
	}

}
